/*
 * Viewer.cpp
 *
 *  Created on: Jun 1, 2011
 *      Author: madboyka
 */

#include "Viewer.h"
#include <iostream>
#include <stdio.h>
#include <cxxtest/TestListener.h>
#include <cxxtest/TestTracker.h>
#include <cxxtest/TestRunner.h>
#include <cxxtest/RealDescriptions.h>
#include <cxxtest/ErrorPrinter.h>
#include "config.h"
#include "ElementData.h"
#include "elementconfig.h"
#include "NetworkTest.h"
ElementData *NetworkTest::edata;

Viewer::Viewer(ViewerProtocol* p)
{
	protocol = p;
	stage = 0;
	errorCount = 0;
}

Viewer::~Viewer()
{
	protocol->close();
	delete protocol;
}

void Viewer::run()
{
	try
	{
		protocol->connect("127.0.0.1");
		if (stage == 0)
		{
			puts("C Sending request...");
			protocol->sendRequest();
			puts("C Awaiting response...");
			protocol->receive();
			puts("C Got response...");
		}
		if (stage == 1)
		{
			puts("Testing in stage 1");
			float mat[] =
			{ 1.500, 0.1321, 0.321321, 0.4433234324, 123320.32132, 1.323323,
					0.6000000, 0.212332121, 0, 0, 1, 0, 1, 1, 1, 1 };
			for (int i = 0; i < DATA_COUNT; ++i)
			{
				protocol->sendRequest();
				protocol->receive();
				float * rec = protocol->getLocationData();
				puts("C Got data, asserting...");
				for (int j = 0; j < 16; ++j)
				{
					//					puts("Asserting stuff...");
					TS_ASSERT_DELTA (mat[j], rec[j], 0.000000001f);
				}
			}
		}
		if (stage == 2)
		{
			puts("Testing in stage 2");
			for (int i = 0; i < DATA_COUNT; ++i)
			{
				puts("Count assert passed...");
				protocol->sendRequest();
				puts("Count assert passed...");
				protocol->receive();
				puts("Count assert passed...");
				TS_ASSERT_EQUALS (protocol->getCount(),1);
				puts("Count assert passed...");
				ElementData ed = protocol->getNewElement(0);
				TS_ASSERT_EQUALS ((*NetworkTest::edata).getDataCount(), ed.getDataCount());
				TS_ASSERT_EQUALS ((*NetworkTest::edata).getElementType(), ed.getElementType());
			}
		}
	} catch (NetworkException ex)
	{
		errorCount++;
		std::cout << ex.showReason().toStdString();
	}
}
